#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022, Andrey Kunitsyn
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

import os

def init(module):
    module.name = ":cmsis:device"

def prepare(module, options):
    device = options[":target"]
    if not device.identifier["platform"] == "rp":
        return False

    module.depends(":cmsis:core")
    return True

def build(env):
    env.collect(":build:path.include", "modm/ext/cmsis_device")

    device = env[":target"]

    env.outbasepath = "modm/ext/cmsis_device"

    defines = []
    headers = [
        device.identifier.string.upper() + ".h"
    ]

    env.copy("pico-sdk/include", "")
    env.copy("address_mapped.h", "hardware/address_mapped.h")

    if env.has_module(":tinyusb"):
        env.substitutions = {
            "with_debug": env.has_module(":debug")
        }
        env.copy("pico.h")
        env.template("pico.cpp.in", "pico.cpp")
        env.copy("irq.h", "hardware/irq.h")
        env.copy("timer.h", "hardware/timer.h")
        env.copy("resets.h", "hardware/resets.h")
        env.copy("sync.h", "hardware/sync.h")

    env.substitutions = {
        "headers": headers,
        "defines": defines,
    }
    env.outbasepath = "modm/src/modm/platform"
    env.template("device.hpp.in")


